/*
 * 项   目：util-mht2hmtl
 * 作   者：罗毅（ymluo）
 * 邮   箱：ymluo@iflytek.com
 * 创建时间：2018/2/1 9:02
 */
package com.roin.util;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.*;

/**
 * 图片和Base64互转工具类
 * @author 罗毅(ymluo)
 * @version 1.0
 * @date 2018-02-01 09:02
 */

public class Img2Base64Util {

    public static void main(String[] args) {
        String str = "R0lGODlhKAAeAIdiAF0jAMush9+RCr6QW/u8H+rITKpgCtGRJNfRzLF0LsRqA+q9M//lR/apCst+COrn5P/RMffZmeCpRsyEGOGl" +
                "J/SkB+rLZ//+x//xYNSFBOquItixa//KJq1oGvbFMteSHvPaTf/cOvPdr97Z1cu1nv/uV+65S/+0DtOGFfX08+qyKPa/JfTRQ/fG" +
                "Pd+pN9+dHOrKXr13FPvlU82ACdSHFb6AKf/7oN2ygf/FH+XXx7aBRsh2DvmyE//2c/e3GrNxHvvNMPjdS+m2Ufn49+SkIuWdEsWe" +
                "bceQPerDPf/bOf/5h/zw1tyvfv/SLeXh3vCyLt2cIv3jTYczAP/CIfLSR71mB9zX08ePQMZvCf3XOeCnVNaOGeWuLtqOCvzLL9/G" +
                "q//sU/rjS/79/MqxltiSE7uRZf/AGtvW0r2ALvPAOe+oEcl+Fvrz5//WM8yHJOGoK+qlF+rNe//OKq5tJPfLN//gQvvhpf/wXv+7" +
                "FfvYQ//pTs1+Ef/9sv/4evW4HPbGWZBOCuTDjODMt9mjQtmkQtygJ3ErAO3MnP/7l869rLqLVv/8Y/fIW9iiYvbKZ+y5NrNyJuKy" +
                "Uufe0PDu7J1JBNKWM//1bf/+0vTy8fbBSP746/rHK+29WPW5M++1Id6xLN+4b+Pf3NuPE7t1G86VQOO3eMJtCtyZF7t9Nfzlr+vK" +
                "ktGvg6dgEMdxBerMb/TPP8N9FPmzFM2jb8GWZtyoYv//z//9uMuJGtWICt+3b9uUGOCgGv/89+jl4vW3KffXReOWCv+/Gf/oS/nc" +
                "m7NvGP/yW/+5E9KJFPvTPf7nTP6zDvzPNP/fReeiF+rFQ+GgIb9qCcVxDNyNB/7iSL6UZvGrFceCFueoJ/rLMfzaPtCBE/i4GvjK" +
                "X+rJUtjTzuu9NP/mSPSoDP/SNcyGHuWmJvWmCf/+yP/LKPjGMP/dPfPesP/uWf62EPjGPuGeHL54FNSIFf/8oP/FIMl4D//3dvS2" +
                "HvvPNOq4Uvv6+v/5iN6yf+rj3OCeIfPTSP/DHQAAACH/C05FVFNDQVBFMi4wAwEAAAAh+QQFCgD/ACwAAAAAKAAeAAAI/wD/CRTI" +
                "KxW3TL46ZXI0bMnAhxAjSnxopwWyOtnqpMtizwMnERNDhuTFaNo0Bsn06GFQJ0QSbI+EiJw5UMyfKNPAIIF2504JaAtCQODgIRLN" +
                "mcPyMADjbFy3Pn0sOZsQtIk8LrmOTtSUJhs0aONgIBqrhN7Urxx8DJIEseDBhAsbQoxAB1wJS0ri8bHFJ54SSyXAhZDjD84GfAPt" +
                "tEPGLOPGZeg4qXtoIpseSgAAUKJU6wIfzJopNcERq1KvfyRNghOmElzLEF4e3Rt4LZ2eO5lt1bp0yRaizHrSjTZ264zNKAz0lOhZ" +
                "bJ0ecOnaEDX6j0g6YXfoIeJj7pJnRH3uCP8TjsOYNQRJl2KwRK8PPUsYwDAIYZULqH/8QtjFq5fvKwCvCCMYYezAwlU2YGSXDyI2" +
                "IKJEDxjoUYd02xAiCRRt1KEHBj0o8ckxvwHyQivESKfPCTHQtV9efLQYTz6WrDNgYRtIsIl+YKyHAgDv7SEDCNbIssYTCjRSWTLF" +
                "aMfHBZ3xgQg94gknDw+VqKJBG+mAo9wdGNwBzhaFFCODC57IUw0t5FyHQR+I2MJbLb71EaGUxLFxABBtJFHHanoICA4WHxTzSzKL" +
                "bKLFM+kkkx0icUBziTnQWBDeeFaZ908guzTRRBshpOMpOOC08cIji5S6CAQQJLEfDG7EwZcrbhT/UMJ8BMLyjxiknCIHB3Jo+tUC" +
                "53BwTBBhQGDqInV8FdZYiOTTzQRftXGOGSgOdAUZK/iDAw7QSMEMB+fokgEuuBh7KktgGGIIVFGpK9hoeISDxkOrjHKMGsYwwww7" +
                "5cygQSmHOEEuqhAwwIC6XPqkbnQcmMFOF0ZAtE8AkHRRQQXSzEENAiNgMhAuXqAqjMHClMxSCG14As8xKGizSkRi5DALJBoncsYD" +
                "ERHBAgSrMZAOSyrAAw8Kb/SzTjFU7DHREL2ccUYvQ0z0DAQ/C1hHHcfIsE4JJazjdQnzaBXSM/WIM1SwzWizxx4UTIPTNNGIPfYz" +
                "fmjrcDjAOLCDOHxjN2OK3CHt88wRNRjAyhw6lBENHFMQIMoNgIeEzyShWIGAN1Y48QU0VVTBhBiRhy766KSXbvpRAQEAIfkEBQoA" +
                "/wAsAwACABgAGAAACLEA/wlcAE0gOIEIEypMiGSCM4HrBKZbSBEatAndevyjR7GjQD0YeuTzSLKkyZMoT8pLyZLki1bEBOpruUcG" +
                "CFiy1jxR0CjllkLFZLjwhKMaLTQpsXz49yvZok1ajHQMkZCBQHcLFmn9BwECSgYMwKEIEsbrv0UpwU3TlQEXrpYCi2godQjVP1xd" +
                "Uzb4l2CMN4TPSP4QUE5gB4r8SAoaMOefIhJw/6VIODmy5cuYMwsUExAAOw==";
        generateImage(str,"D:\\test.gif");
    }
    /**
     * 将base64编码字符串转换为图片
     * @param imgStr base64编码字符串
     * @param path 图片路径-具体到文件
     * @return boolean
     * @throws
     * @author 罗毅(ymluo)
     * @date 2018-02-01 09:06
     */
    public static boolean generateImage(String imgStr, String path) {
        if (imgStr == null)
            return false;
        OutputStream out=null;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // 解密
            byte[] b = decoder.decodeBuffer(imgStr);
            // 处理数据
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            out = new FileOutputStream(path);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }finally {
            if (out!=null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    /**
     * 将图片转成base64编辑字符串
     * @param imgFile 图片路径-具体到文件
     * @return java.lang.String
     * @throws
     * @author 罗毅(ymluo)
     * @date 2018-02-01 09:06
     */
    public static String getImageStr(String imgFile) {
        InputStream inputStream = null;
        byte[] data = null;
        try {
            inputStream = new FileInputStream(imgFile);
            data = new byte[inputStream.available()];
            inputStream.read(data);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (inputStream!=null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        // 加密
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }
}
